package com.cj.cms.web.security;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.cms.entity.LeaveWord;
import com.cj.cms.service.LeaveWordManager;
import com.cj.common.orm.Page;
import com.cj.common.orm.PropertyFilter;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.orm.hibernate.HibernateWebUtils;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;
import com.cj.sys.service.security.UserManager;

public class LeaveWordAction extends CRUDActionSupport<LeaveWord>{
	@Autowired
	private LeaveWordManager leaveWordManager;
	
	protected Page<LeaveWord> page = new Page<LeaveWord>(25,"createTime",Page.DESC);

	@Autowired
	private UserManager userManager;
	
	private int leaveWordStatus;
	
	private String resolventStr;
	protected void prepareModel() throws Exception{
		if(id != null)
			entity = leaveWordManager.get(id);
		else
			entity = new LeaveWord();
	}
	
	@Override
	public String list() {
		try {
			HttpServletRequest request = Struts2Utils.getRequest();
			List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(request);
			page = getEntityManager().search(page, filters);
			return SUCCESS;
		} catch (Exception e) {
			setErrMessage(e.getMessage(),e);
			return ERROR;
		}
	}
	
	public String resolvent(){
		try {
			entity = leaveWordManager.get(id);
			entity.setReplayTime(new Date());
			entity.setReplayUser(userManager.getCurrentUser());
			entity.setStatus(1);
			entity.setResolvent(resolventStr);
			leaveWordManager.save(entity);
			return list();
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	public String publish(){
		try {
			leaveWordManager.publish(id);
			Struts2Utils.renderText("true");
		} catch (Exception e) {
			setErrMessage(e.getMessage());
			Struts2Utils.renderText("error:" + getErrMessage());
		}
		return null;
	}
	@Override
	public EntityManager getEntityManager() {
		return leaveWordManager;
	}

	public Page<LeaveWord> getPage() {
		return page;
	}

	public void setPage(Page<LeaveWord> page) {
		this.page = page;
	}

	public String getResolventStr() {
		return resolventStr;
	}

	public void setResolventStr(String resolventStr) {
		this.resolventStr = resolventStr;
	}
	
}
